/**
 * Copyright (c) 2010 Basil Shikin, BugStat Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.bugstat.renderers;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bugstat.common.reports.Report;
import org.bugstat.common.utils.AnnotatedClassLoader;
import org.bugstat.common.utils.FractionedProgressListener;
import org.bugstat.common.utils.ProgressListener;

/**
 * This class is used to manage available report renderers.
 * 
 * @author Basil Shikin
 */
public class RendererManager
{
    private static Log log = LogFactory.getLog(RendererManager.class);

    static
    {
        // Scan our source base for annotated classes
        AnnotatedClassLoader.scanBase(RendererManager.class);
    }

    /**
     * Render all given report on all renders
     * 
     * @param reports
     *            Reports to render. Must not be null.
     * @param reportInformation
     *            General information about reports to render. Must not be null.
     * @param outputFolder
     *            Folder to put report into. Must not be null.
     * @param renderers
     *            Renderers to render report with. Must not be null.
     * @param progressListener
     *            Render progress listener.
     * @throws IOException
     *             In case unable to render at least one report.
     */
    public void renderReports(final List<Report> reports, final ReportInformation reportInformation, final File outputFolder, final Collection<ReportRenderer> renderers, final ProgressListener progressListener)
            throws IOException
    {
        log.info("Rendering " + reports.size() + " generated reports with " + renderers.size() + " renderers into " + outputFolder.getAbsolutePath() + "...");
        
        final FractionedProgressListener progressListenerWrapper = new FractionedProgressListener( progressListener, renderers.size() );
        
        for (ReportRenderer renderer : renderers)
        {
            log.debug("Rendering " +  reports.size() + " reports with " + renderer);

            renderer.renderReports(reports, reportInformation, outputFolder, progressListenerWrapper );
        }
        
        log.info(reports.size() + " reports rendered");
    }

    /**
     * List all available renderers
     * 
     * @return All available renderers.
     */
    public static Collection<ReportRenderer> listRenderers()
    {
        return AnnotatedClassLoader.retrieveClassesWithAnnotation(Renderer.class, ReportRenderer.class);
    }
}
